package com.shangs.platform.modular.system.controller;

import com.shangs.platform.common.annotion.Permission;
import com.shangs.platform.common.constant.Const;
import com.shangs.platform.common.constant.code.YesOrNo;
import com.shangs.platform.common.exception.BizExceptionEnum;
import com.shangs.platform.common.exception.BussinessException;
import com.shangs.platform.common.persistence.dao.AppVersionMapper;
import com.shangs.platform.common.persistence.model.AppVersion;
import com.shangs.platform.core.base.controller.BaseController;
import com.shangs.platform.core.base.tips.Tip;
import com.shangs.platform.core.log.LogObjectHolder;
import com.shangs.platform.core.support.BeanKit;
import com.shangs.platform.core.util.ToolUtil;
import com.shangs.platform.modular.system.dao.AppDao;
import com.shangs.platform.modular.system.service.IAppService;
import com.shangs.platform.modular.system.warpper.AppWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 河长制app控制器
 *
 * @author shangs
 * @Date 2017-10-23 01:58:44
 */
@Controller
@RequestMapping("/app")
public class AppController extends BaseController {

    @Resource
    AppVersionMapper appVersionMapper;
    @Resource
    AppDao appDao;
    @Resource
    IAppService appService;

    private String PREFIX = "/system/app/";

    /**
     * 跳转到河长制app首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "app.html";
    }

    /**
     * 跳转到添加河长制app
     */
    @RequestMapping("/app_add")
    public String appAdd() {
        return PREFIX + "app_add.html";
    }

    /**
     * 跳转到修改河长制app
     */
    @RequestMapping("/app_update/{appId}")
    public String appUpdate(@PathVariable Integer appId, Model model) {
        if (ToolUtil.isEmpty(appId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        AppVersion appVersion = this.appDao.selectByPrimaryKey(appId);
        Map<String, Object> appMap = BeanKit.beanToMap(appVersion);
        model.addAttribute("app", appMap);
        LogObjectHolder.me().set(appMap);
        return PREFIX + "app_edit.html";
    }

    /**
     * 获取河长制app列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) String beginTime,
                       @RequestParam(required = false) String endTime) {
        List<Map<String, Object>> apps = this.appDao.selectByCondition(new AppVersion());
        for (int i = 0; i < apps.size(); i++) {
            Map<String, Object> app = apps.get(i);
            if (app.get("status") != null && YesOrNo.isYes(app.get("status").toString())) {
                app.put("publishStatusName", YesOrNo.YesStr);
            } else {
                app.put("publishStatusName", YesOrNo.NoStr);
            }
            if (app.get("is_force") != null && YesOrNo.isYes(app.get("is_force").toString())) {
                app.put("isForceName", YesOrNo.YesStr);
            } else {
                app.put("isForceName", YesOrNo.NoStr);
            }
            apps.set(i, app);
        }
        return new AppWarpper(apps).warp();
    }

    /**
     * 新增河长制app
     */
    @Permission(Const.ADMIN_NAME)
    @RequestMapping(value = "/add")
    //@BussinessLog(value = "新增河长app发布记录", key = "content", dict = AppDict.class)
    @ResponseBody
    public Object add(AppVersion appVersion) {
        appVersion.setCreateTime(new Date());
        this.appDao.insert(appVersion);
        return SUCCESS_TIP;
    }

    /**
     * 删除河长制app
     */
    @Permission(Const.ADMIN_NAME)
    @RequestMapping(value = "/delete")
    //@BussinessLog(value = "删除河长app发布记录", key = "content", dict = AppDict.class)
    @ResponseBody
    public Object delete(@RequestParam Integer appId) {
        this.appDao.deleteByPrimaryKey(appId);
        return SUCCESS_TIP;
    }


    /**
     * 修改河长制app
     */
    @Permission(Const.ADMIN_NAME)
    @RequestMapping(value = "/update")
    //@BussinessLog(value = "修改河长app发布记录", key = "content", dict = AppDict.class)
    @ResponseBody
    public Tip add(@Valid AppVersion appVersion, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.appDao.updateByPrimaryKey(appVersion);
        return SUCCESS_TIP;
    }


    /**
     * 河长制app发布详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Tip detail(@PathVariable Integer appId) {
        if (ToolUtil.isEmpty(appId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.appDao.selectByPrimaryKey(appId);
        return SUCCESS_TIP;
    }

    /**
     * 发布河长制app
     */
    @Permission(Const.ADMIN_NAME)
    @RequestMapping(value = "/publish")
    //@BussinessLog(value = "河长app发布", key = "content", dict = AppDict.class)
    @ResponseBody
    public Object publish(@RequestParam Integer appId) {
        this.appDao.publishById(appId);
        return SUCCESS_TIP;
    }
}
